package in.muton.panels.paginator.components.badges;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.coursepages.CourseDept;
import in.muton.edushare.pages.coursepages.CourseUniv;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLHelper;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.panels.paginator.badgehelpers.course.CourseBadgeInterface;
import in.muton.panels.paginator.helpers.deletelinks.CourseDelete;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;

public class CourseBadge extends EdusharePanel {

	public CourseBadge(String id, EduShareBasePage page,
			IModel<CourseInstances> instModel) {
		super(id, instModel, page);
		this.instModel = instModel;
		this.courseModel = new CayenneModel<Courses>(instModel.getObject()
				.getCourse());
		init();
	}

	public CourseBadge(String id, EduShareBasePage page, IModel<Courses> model,
			CourseBadgeInterface bif) {
		super(id, model, page);
		courseModel = model;
		this.bif = bif;
		init();
	}

	private void init() {
		add(CSSPackageResource.getHeaderContribution(CourseBadge.class,
				"badge.css"));
		PageParameters p = null;
		if (instModel != null)
			p = EntityPageParams.getParams(instModel);
		else
			p = EntityPageParams.getParams(courseModel);

		Link<Void> l = new BookmarkablePageLink<Void>("imgLink", URLHelper
				.getCourseHomeClass(), p);
		add(l);
		l.add(new ProfileImage("img", courseModel,
				ApplicationConstants.FILE_SUFFIX_ICON));
		l = new BookmarkablePageLink<Void>("nameLink", URLHelper
				.getCourseHomeClass(), p);
		l.add(new Label("name", StringUtil.ellipsis(courseModel.getObject()
				.getCourseName(), ApplicationConstants.TAG_BADGE_CHAR_LIMIT)));
		l.add(new SimpleAttributeModifier("title", courseModel.getObject()
				.getCourseName()));
		add(l);

		if (EduShareSession.get().isLoggedIn()) {
			add(new BookmarkablePageLink<Void>("univ", CourseUniv.class, p));
			add(new BookmarkablePageLink<Void>("dept", CourseDept.class, p));
		} else {
			PageParameters param = null;
			if (courseModel.getObject().getUniversities() != null
					&& courseModel.getObject().getUniversities().size() > 0) {
				param = EntityPageParams
						.getParams(new CayenneModel<Universities>(courseModel
								.getObject().getUniversities().get(0)
								.getUniversity()));
			}
			add(new BookmarkablePageLink<Void>("univ", URLHelper
					.getUnivHomeClass(), param));
			if (courseModel.getObject().getDeptLinks() != null
					&& courseModel.getObject().getDeptLinks().size() > 0) {
				param = EntityPageParams
						.getParams(new CayenneModel<Departments>(courseModel
								.getObject().getDeptLinks().get(0)
								.getDepartment()));
			}
			add(new BookmarkablePageLink<Void>("dept", URLHelper
					.getDepartmentHomeClass(), param));
		}

		add(new BookmarkablePageLink<Void>("people", URLHelper
				.getCoursePeoplePageClass(), p));
		add(new CourseDelete("delete", this, bif, courseModel));
	}

	private IModel<Courses> courseModel = null;
	private IModel<CourseInstances> instModel = null;
	private CourseBadgeInterface bif = null;
	private static final long serialVersionUID = -5105246659921309131L;
}
